package be.djdb.game.othello;
import java.awt.Dialog;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import be.djdb.generators.MYFORM;
import be.djdb.generators.Menu;
/**
* @author Lieven Roegiers
* @copyright 2012
* @from JAVA_othello
* @repos http://code.google.com/p/java-hogeschool-projecten/
*/
public class Main extends MYFORM{
	private Dialog Settingframe;
	private ActionListener saveas;
	private JFileChooser fc ;
	private ActionListener save;
	private ActionListener savesettings;
	private ActionListener options;
	private ActionListener about;
	private ActionListener newgame;
	JInternalFrame inner;
	Bord bord;
	boolean isGekleurdAanBeurt = true;
	private Settings setting=  new Settings();
	private ActionListener optionsb;
	public Main() {//Settings
		super(Statics.PROJECTNAME,Statics.RESOURCENAME,false);
		gui();
	}
	private void gui(){
		 	if (Statics.DEBUG){log(Level.WARNING, "LOCAL_GUIsetup" );}
		 	getSettings();
			actions();
			init();
			menusetup();
	}
	public Settings getsettings(){
		return setting;
	}
	private void menusetup(){
        Menu menu= new Menu(Statics.RESOURCENAME);
     	menu.addmenuitem("File","newgame",new JMenuItem(),newgame);
     	menu.addmenuitem("File","exit",new JMenuItem(),savesettings);
     	menu.addmenuitem("Tools","options",new JMenuItem(),options);

     	menu.addmenuitem("Help","Help",new JMenuItem(),about);
		setJMenuBar(menu.getmenubar());
    }
	private void actions(){
		fc = new JFileChooser();
	   

		options = new ActionListener() {
			private OptiePanel panel;
			 JDialog dialog;
			private ActionListener ok;
			private ActionListener cancel;
	        final int DWIDTH = 200;
	        final int DHEIGHT = 240;
			public void actionPerformed(ActionEvent e) {
			        panel = new OptiePanel(getsettings());		        
			        ok =new ActionListener (){
			            @Override public void actionPerformed (ActionEvent a) {
			                if(!panel.getSettings().isconsitent()){
			                    return;
			                }
			                setting = panel.getSettings();    
			                dialog.dispose ();
			            }
			        };
			        cancel =new ActionListener (){
			            @Override public void actionPerformed (ActionEvent a){
			                dialog.dispose ();
			            }
			        };
			        panel.initComponents(ok, cancel);
			        dialog = new JDialog (Main.this, "Information", true);
			        dialog.setSize (DWIDTH, DHEIGHT);
			        dialog.setResizable (false);
			        dialog.setDefaultCloseOperation (JDialog.DISPOSE_ON_CLOSE);
			        dialog.setContentPane (panel);
			        dialog.setLocationRelativeTo (Main.this);
			        dialog.setVisible (true);
			}
		};
	    save = new ActionListener(){
	    	public void actionPerformed(ActionEvent evt) {
	    		
	    	}
	    };
	    newgame = new ActionListener(){
	    	public void actionPerformed(ActionEvent evt) {
	    		System.out.println("new game event");
    			bord.reset(setting.getbordsizeX(), setting.getbordsizeY());
			}
	    };
	}
	private void setsettings(OptiePanel panel){
		setting = panel.getSettings();
	}
	private void init() {
		//System.out.println("initalize product");
		try {
			bord = new Bord(setting.getbordsizeX(),setting.getbordsizeX(),setting.getblockSize());
			if(bord.isgeldigezet(2, 3, true)){
				//System.out.println("is geldige zet");
			}else{
				//System.out.println("is geen geldige zet");
			}
			//bord.toString();
			inner=  new JInternalFrame("Beste speler u kan beginnen door op één vak te klikken en een kleur in te sluiten");
			inner.setSize(800,750);
			inner.add(bord);
			add(inner);
			
		} catch (OutOfBoundException e) {
			e.printStackTrace();
		}
	    setDefaultCloseOperation(EXIT_ON_CLOSE);
	    setSize(22*25+40+110,22*25+110);
	    //setLocationRelativeTo(null);
	    setVisible(true);
	}
	public static void main(String[] args) {
        java.awt.EventQueue.invokeLater(new Runnable(){
            public void run(){
            	new Main().setVisible(true);
            }
        });
	}
    private void log(Level level, String msg) {
	    String tag ="<>>>>*---------------othelo--------------*<<<<>";
	    Logger.getLogger(Main.class.getName()).log(level, tag + msg);
	}
    private Settings getSettings(){
    	if(this.setting == null){
    		return new Settings();
    	}else{
    		return this.setting;
    	}
		
	}
	public void setsettings(Settings settings) {
		this.setting =settings;
		
	}
}
